﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Training.Domain;
using Training.Domain.Formatters;
using Training.Domain.Repositories;
using Training.Utils;

namespace Training.Data.Repositories
{
    public class PersonCsvRepository : GenericCsvRepository<Person>, IPersonRepository
    {
        public void Remove(string vat)
        {
            var person = GetAll().SingleOrDefault(p => p.Vat == vat);
            Remove(person.Id);
        }

        protected override string GetString(Person person)
        {
            return string.Format("{0};{1};{2};{3};{4};{5}",
                person.Id,
                person.Vat,
                person.Name,
                person.Surname,
                person.Birthdate,
                person.Salary);
        }

        protected override Person GetEntity(string line)
        {
            var lineCsv = line.Split(';');
            return new Person()
            {
                Id = new Guid(lineCsv.ElementAt(0)),
                Vat = lineCsv.ElementAt(1),
                Name = lineCsv.ElementAt(2),
                Surname = lineCsv.ElementAt(3),
                Birthdate = DateTime.Parse(lineCsv.ElementAt(4)),
                Salary = decimal.Parse(lineCsv.ElementAt(5))
            };
        }
    }
}
